//引入公共样式
require("../../assets/css/basic.less");
require("../../assets/css/normalize.css");
require("../../assets/css/property.less");
require("../../assets/css/reset.css");
require("./register.less");
// 引入公用js模块
let dom = require("../../utils/dom.js");
let validator = require("../../utils/validator.js");
let https = require("../../utils/https.js");
// 引入模块
var CaptchaMini = require("captcha-mini");
window.addEventListener("DOMContentLoaded", function () {
  //切页面
  dom.jump("#jump", "login");
  //功能一:验证使用
  let vcodeJs = "";
  /*不传值，统一走默认值*/
  let captcha = new CaptchaMini({
    fontSize: 30,
  });
  captcha.draw(dom.getEle("#captcha1"), (r) => {
    // console.log(r, '验证码1');
    vcodeJs = r.toLowerCase();
  });

  dom.getEle("#submit").addEventListener("click", function () {
    //获取所有字段的值
    let account = dom.getEle("#phone").value;
    let vcodeVal = dom.getEle("#captchaIpt").value;
    let password = dom.getEle("#password").value;
    let okPwdVal = dom.getEle("#checkPassword").value;

    if (account == "") {
      //手机号-非空
      alert("请输入手机号");
      return;
    } else if (!validator.cheUser(account)) {
      //手机号-正则
      alert("你的手机号码不正确!");
      return;
    } else if (vcodeVal == "") {
      //验证码-非空
      alert("请输入验证码");
      return;
    } else if (vcodeVal.toLowerCase() != vcodeJs) {
      //验证码-不匹配
      alert("验证码输入有误!");
      return;
    } else if (password == "") {
      //密码-非空
      alert("请输入密码");
      return;
    } else if (!validator.chePas(password)) {
      //密码-正则
      alert("你的密码不正确!");
      return;
    } else if (okPwdVal != password) {
      //确认密码和密码是否匹配
      alert("你的两次密码不一致!");
      return;
    } else {
      https.post("/api/user/isExist", { account }, function (res) {
        console.log(res);
        if (res.errno == 0) {
          https.post(
            "/api/user/register",
            { account, password },
            function (res) {
              if (res.errno == 0) {
                alert("恭喜您!注册成功!");
                location.href = "login.html";
              } else {
                alert("注册失败!");
              }
            }
          );
        } else {
          https.post("/api/user/isExist", { account }, function (res) {
            if (res.errno == 0) {
              https.post(
                "/api/user/register",
                { account, password },
                function (res) {
                  if (res.errno == 0) {
                    alert("恭喜您!注册成功!");
                    location.href = "login.html";
                  } else {
                    alert("注册失败!");
                  }
                }
              );
            } else {
              alert("这个号已存在,请开会员购买新账号！！！");
            }
          });
        }
      });
    }
  });
});
